#pragma once

class CDevice
{
private:
	LPDIRECT3D9				m_pD3D;
	LPDIRECT3DDEVICE9		m_pDevice;
	D3DPRESENT_PARAMETERS	m_d3dpp;
	D3DCAPS9				m_Caps;
	vector<D3DLIGHT9>		m_Light;
	LPD3DXSPRITE			m_pSprite;
	LPDIRECT3DSURFACE9		m_pSurface;
	HRESULT					m_hResultSprite;
	LPDIRECT3DSURFACE9		m_OrgRenderTarget;
	LPDIRECT3DSURFACE9		m_OrgDepthStencil;
	BOOL					m_bLostDevice;

	CDevice()
	{
		m_OrgRenderTarget = NULL;
		m_OrgDepthStencil = NULL;
	}

public:
	static CDevice &GetInstance()
	{
		static CDevice Instance;
		return Instance;
	}

	BOOL InitDevice(HWND _hWnd, int _Width, int _Height, BOOL _FullScreen);
	void ReleaseDevice();
	inline LPDIRECT3DDEVICE9 GetDevice(){ return m_pDevice; }
	inline LPD3DXSPRITE GetSprite(){ return m_pSprite; }
	DWORD Float2DWORD( float val ) { return *((DWORD*)&val); }

	
	inline void ResetRenderTarget()
	{
		m_pDevice->SetRenderTarget( 0, m_OrgRenderTarget );
	}
	inline void ResetDepthStecil()
	{
		m_pDevice->SetDepthStencilSurface( m_OrgDepthStencil );
	}

	inline void BlendAlpha();
	inline void BlendAdd();
	
	void SetWorldMatrix(const D3DXMATRIX& _Mat);
	void SetTex( LPDIRECT3DTEXTURE9 _pTex, int _pass = 0 );

	
	void SetState();

	void BeginScene();
	void EndScene();
	void BeginSprite();
	void EndSprite();

};

#define DX CDevice::GetInstance()
#define DEVICE CDevice::GetInstance().GetDevice()
#define SPRITE CDevice::GetInstance().GetSprite()